// 安全地创建Canvas - 小程序环境优化
let canvas = null;

// 在小程序环境中，Canvas由页面提供，不在这里创建
console.log('Canvas将在页面中初始化');

// 安全地获取窗口信息 - 小程序环境优化
let windowInfo;
try {
  if (typeof wx !== 'undefined') {
    // 优先使用新API
    if (wx.getWindowInfo) {
      windowInfo = wx.getWindowInfo();
    } else if (wx.getDeviceInfo) {
      windowInfo = wx.getDeviceInfo();
    } else if (wx.getSystemInfoSync) {
      // 降级到旧API
      windowInfo = wx.getSystemInfoSync();
    } else {
      throw new Error('无法获取窗口信息');
    }
  } else {
    throw new Error('微信环境不可用');
  }
} catch (error) {
  console.warn('获取窗口信息失败:', error);
  // 使用默认值
  windowInfo = {
    screenWidth: 375,
    screenHeight: 667
  };
}

// 确保屏幕尺寸有效
const getScreenWidth = () => {
  if (windowInfo && windowInfo.screenWidth) {
    return windowInfo.screenWidth;
  }
  if (windowInfo && windowInfo.windowWidth) {
    return windowInfo.windowWidth;
  }
  return 375;
};

const getScreenHeight = () => {
  if (windowInfo && windowInfo.screenHeight) {
    return windowInfo.screenHeight;
  }
  if (windowInfo && windowInfo.windowHeight) {
    return windowInfo.windowHeight;
  }
  return 667;
};

export const SCREEN_WIDTH = getScreenWidth();
export const SCREEN_HEIGHT = getScreenHeight();

// 安全地获取Canvas上下文
let ctx = null;
console.log('Canvas上下文将在页面中初始化');
export { ctx };